/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include "arch_c_compat.h"

using namespace sleepyface;
using namespace sleepyface::arch;

Architecture *_arch;

int z_value_get_type(ModuleValue *v)
{
    return v->getType();
}

int z_value_get_bool(ModuleValue *v)
{
    return v->getBool();
}

int z_value_get_int(ModuleValue *v)
{
    return v->getInt();
}

char *z_value_get_string(ModuleValue *v)
{
    return (char *)v->getString().c_str();
}

void *z_value_get_data(ModuleValue *v)
{
    return v->getData();
}

ModuleValue *z_value_new_int(int v)
{
    return new ModuleValue(ModuleValue::_int, v);
}

ModuleValue *z_value_new_string(char *v)
{
    return new ModuleValue(ModuleValue::_string, string(v));
}

ModuleValue *z_value_new_data(void *v)
{
    return new ModuleValue(ModuleValue::_data, v);
}

int z_arch_signal0(char *m, int s)
{
    if (!_arch->isLoaded(string(m)))
        return -1;
    ModuleSignal sig = ModuleSignal(s);
    return _arch->module(string(m))->signal(sig, *_arch);
}

int z_arch_signal1(char *m, int s, ModuleValue *a0)
{
    if (!_arch->isLoaded(string(m)))
        return -1;
    ModuleSignal sig = ModuleSignal(s);
    sig.append(*a0);
    return _arch->module(string(m))->signal(sig, *_arch);
}

int z_arch_signal2(char *m, int s, ModuleValue *a0, ModuleValue *a1)
{
    if (!_arch->isLoaded(string(m)))
        return -1;
    ModuleSignal sig = ModuleSignal(s);
    sig.append(*a0);
    sig.append(*a1);
    return _arch->module(string(m))->signal(sig, *_arch);
}

ModuleValue *z_arch_get(char *m, char *k0, char *k1)
{
    if (!_arch->isLoaded(string(m)))
        return NULL;
    return new ModuleValue(_arch->module(string(m))->get(string(k0), string(k1), *_arch));
}

ModuleValue *z_arch_set(char *m, char *k0, char *k1, ModuleValue *v)
{
    if (!_arch->isLoaded(string(m)))
        return NULL;
    return new ModuleValue(_arch->module(string(m))->set(string(k0), string(k1), *v, *_arch));
}

char **z_arch_keylist(char *m)
{
    if (!_arch->isLoaded(string(m)))
        return NULL;

    vector<string> tmp = _arch->module(string(m))->list(*_arch);
    char **rtn = (char **)malloc(sizeof(char *)*(tmp.size()+1));
    memset(rtn, 0, sizeof(char *)*(tmp.size()+1));
    for (int i = 0; i < tmp.size(); i++)
        rtn[i] = (char *)tmp[i].c_str();
    return rtn;
}

int z_arch_is_loaded(char *m)
{
    return (int)_arch->isLoaded(string(m));
}

char **z_arch_list()
{
    vector<string> tmp = _arch->list();
    char **rtn = (char **)malloc(sizeof(char *)*(tmp.size()+1));
    memset(rtn, 0, sizeof(char *)*(tmp.size()+1));
    for (int i = 0; i < tmp.size(); i++)
        rtn[i] = (char *)tmp[i].c_str();
    return rtn;
}

int z_arch_get_memory_usage()
{
    return _arch->getMemoryUsage();
}
